package com.sk.waste.app.adapters;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.sk.waste.app.R;
import com.sking.album.imageload.SKImageTask;
import com.sking.album.imageload.SKImageWorker;
import com.sking.album.model.SKRImageModel;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @Created by Im_Jingwei
 * @time 2020/6/11  16:10
 * @Describe 相册
 */
public class AlbumEditAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
  {

    private static final int TYPE_ADD = 0;
    private static final int TYPE_IMAGE = 1;
    private Context mContext;
    private int stateType;//adapter显示状态，0不可编辑，1可编辑
    private ArrayList<SKRImageModel> list;
    private SKImageWorker imageWorker;

    public AlbumEditAdapter(Context mContext, int stateType, ArrayList<SKRImageModel> list)
      {
        this.mContext = mContext;
        this.stateType = stateType;
        this.list = list;
        imageWorker = new SKImageWorker(mContext);
      }

    @Override
    public int getItemCount()
      {
        return list.size() + 1;
      }

    @Override
    public int getItemViewType(int position)
      {
        return position == list.size() ? TYPE_ADD : TYPE_IMAGE;
      }

    @Override
    public long getItemId(int position)
      {
        return 0;
      }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType)
      {
        if (viewType == TYPE_ADD)
          {
            return new AddButtHolder(LayoutInflater.from(mContext).inflate(R.layout.item_album_add_butt, viewGroup, false));
          } else
          {
            return new ItemHolder(LayoutInflater.from(mContext).inflate(R.layout.item_album_image, viewGroup, false));
          }
      }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position)
      {
        if (viewHolder instanceof AddButtHolder)
          {
            setDataAdd((AddButtHolder) viewHolder, position);
          } else
          {
            setDataImage(position, (ItemHolder) viewHolder);
          }
      }

    private void setDataAdd(AddButtHolder holder, final int position)
      {
        if (stateType == 0)
          {
            holder.addButton.setVisibility(View.GONE);
          } else
          {
            holder.addButton.setVisibility(View.VISIBLE);
          }
        holder.addButton.setOnClickListener(new View.OnClickListener()
          {
            @Override
            public void onClick(View v)
              {
                mOnItemClickLitener.onItemClick(position);
              }
          });
      }

    private void setDataImage(final int position, ItemHolder holder)
      {
        if (stateType == 0)
          {
            holder.deleteButt.setVisibility(View.GONE);
          } else
          {
            holder.deleteButt.setVisibility(View.VISIBLE);
          }
        Log.d("position", "position " + position);
        String urlPath = list.get(position).getImgurl();
        if (TextUtils.isEmpty(urlPath))
          return;
        if (urlPath.startsWith("https://") || urlPath.startsWith("http://"))//网络图片
          {
            try
              {
                SKImageTask loadtask = new SKImageTask(holder.imageView, new URL(list.get(position).getImgurl()), mContext);
                imageWorker.loadImage(loadtask);
              } catch (MalformedURLException e)
              {
                e.printStackTrace();
              }
          } else//本地图片
          {
            SKImageTask imageTask = new SKImageTask(holder.imageView, list.get(position).getImgurl(), mContext);
            imageWorker.loadImage(imageTask);
          }
        holder.imageView.setOnClickListener(new View.OnClickListener()
          {
            @Override
            public void onClick(View v)
              {
                mOnItemClickLitener.onItemClick(position);
              }
          });
        holder.deleteButt.setOnClickListener(new View.OnClickListener()
          {
            @Override
            public void onClick(View v)
              {
                mOnItemClickLitener.onItemDelete(position);
              }
          });
      }

    /**
     * ItemClick的回调接口
     */
    public interface OnItemClickLitener
      {
        void onItemClick(int position);

        void onItemDelete(int position);
      }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
      {
        this.mOnItemClickLitener = mOnItemClickLitener;
      }

    /**
     * addbuttview
     */
    public class AddButtHolder extends RecyclerView.ViewHolder
      {
        private Button addButton;

        public AddButtHolder(View itemView)
          {
            super(itemView);
            addButton = itemView.findViewById(R.id.album_add_button);
          }
      }

    /**
     * imgView
     */
    public class ItemHolder extends RecyclerView.ViewHolder
      {
        private ImageView imageView;
        private ImageButton deleteButt;

        public ItemHolder(View itemView)
          {
            super(itemView);
            imageView = itemView.findViewById(R.id.album_item_image_view);
            deleteButt = itemView.findViewById(R.id.album_item_image_delete);
          }
      }
  }
